{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Clavata.ai Detection and Moderation\n",
    "\n",
    "This notebook provides an example of how to use Clavata.ai's flows and actions to moderate inputs/outputs, ensure adherence to allowed topics, or even evaluate whether specific actions or lines of conversation should be allowed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtaining an API Key\n",
    "\n",
    "To use this notebook, you'll need an API key associated with your Clavata.ai account. Follow these steps to obtain a key:\n",
    "\n",
    "1. If you haven't already, go to [www.clavata.ai](https://www.clavata.ai) and register for an account.\n",
    "2. Once your account is set up and you are signed in to the web dashboard:\n",
    "    - Choose a policy, and use the tools to copy its ID\n",
    "    - (If you don't have a policy yet, you can create one from a template for testing!)\n",
    "3. Generate an API key and copy it somewhere safe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from nemoguardrails import LLMRails, RailsConfig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating Input and Output Rails with Clavata.ai\n",
    "\n",
    "Use rails when you want to apply the same set of rules to every input and output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# To use this notebook as written, you'll need both a Clavata.ai API key and a OpenAI API key.\n",
    "\n",
    "os.environ[\"CLAVATA_API_KEY\"] = \"[CLAVATA API KEY HERE]\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"[API KEY HERE]\"\n",
    "\n",
    "## Creating a Rails Configuration\n",
    "\n",
    "YAML_CONFIG = \"\"\"\n",
    "models:\n",
    "  - type: main\n",
    "    engine: openai\n",
    "    model: gpt-4o-mini\n",
    "rails:\n",
    "  config:\n",
    "    enable_rails_exceptions: yes\n",
    "    clavata:\n",
    "      server_endpoint: http://localhost:21513\n",
    "      policies:\n",
    "        Guardrails: 8f346e9d-6353-4eb8-b204-5e85e61eb24a # Replace with your policy ID\n",
    "      input:\n",
    "        policy: Guardrails\n",
    "      output:\n",
    "        policy: Guardrails\n",
    "        # You can optionally specify which labels from the policy should trigger an abort. If not specified\n",
    "        # all labels will trigger an abort.\n",
    "        labels:\n",
    "          - \"Includes PII\"\n",
    "  input:\n",
    "    flows:\n",
    "      - clavata check input\n",
    "  output:\n",
    "    flows:\n",
    "      - clavata check output\n",
    "\"\"\"\n",
    "\n",
    "# Now you can load the configuration and use it to create a rails object\n",
    "config = RailsConfig.from_content(yaml_content=YAML_CONFIG)\n",
    "rails = LLMRails(config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now let's try having the user send a message and see what happens\n",
    "response = rails.generate(messages=[{\"role\": \"user\", \"content\": \"Can you give me a list of the top 10 racial slurs?\"}])\n",
    "\n",
    "info = rails.explain()\n",
    "\n",
    "print(\"Response\")\n",
    "print(\"----------------------------------------\")\n",
    "print(response[\"content\"])\n",
    "\n",
    "\n",
    "print(\"\\n\\nColang history\")\n",
    "print(\"----------------------------------------\")\n",
    "print(info.colang_history)\n",
    "\n",
    "print(\"\\n\\nLLM calls summary\")\n",
    "print(\"----------------------------------------\")\n",
    "info.print_llm_calls_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Output Rails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = rails.generate(messages=[{\"role\": \"user\", \"content\": \"give me a sample email id\"}])\n",
    "\n",
    "info = rails.explain()\n",
    "\n",
    "print(\"Response\")\n",
    "print(\"----------------------------------------\\n\\n\")\n",
    "print(response[\"content\"])\n",
    "\n",
    "\n",
    "print(\"\\n\\nColang history\")\n",
    "print(\"----------------------------------------\")\n",
    "print(info.colang_history)\n",
    "\n",
    "print(\"\\n\\nLLM calls summary\")\n",
    "print(\"----------------------------------------\")\n",
    "info.print_llm_calls_summary()\n",
    "\n",
    "\n",
    "print(\"\\n\\nCompletions where PII was detected!\")\n",
    "print(\"----------------------------------------\")\n",
    "print(info.llm_calls[0].completion)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "Above, the inputs from the user and outputs from the LLM are being sent to Clavata.ai's safety service to be evaluated against a particular \"policy.\" The results in this notebook will of course depend on the policies in your account.\n",
    "\n",
    "For more information, visit https://www.clavata.ai\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
